package com.liu.project.config;


import com.liu.project.utils.JwtUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.context.request.RequestScope;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static java.util.Collections.singletonList;
import static org.assertj.core.util.Lists.newArrayList;

/**
 * @program: project
 * @description:
 * @author: 86187
 * @create: 2022-01-05 15:54
 **/

@Configuration
@EnableOpenApi
@EnableSwagger2
public class SwaggerConfig {
    //配置了swagger的Docket的bean实例


    @Bean
    public Docket docket(Environment environment) {


        //配置要现实的Swagger环境
        Profiles profiles = Profiles.of("develop", "produce");
        //通过environment.acceptsProfiles 判断是否处于设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .enable(!flag)
                .securityContexts(Collections.singletonList(securityContext()))
                .securitySchemes(Collections.singletonList(apiKey()))
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.liu.project.controller"))
                .paths(PathSelectors.any())
//                .build()
//                .securityContexts(Arrays.asList(SecurityContext.builder()
//                        .securityReferences(Arrays.asList(SecurityReference.builder()
//                                .reference("token")
//                                .scopes(new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")})
//                                .build()))
//                        .build()))
//                .securitySchemes(Arrays.asList(new ApiKey("Authorization", "Authorization", "header")))
//                .select()
//                .paths(PathSelectors.regex("^(?!/error).*"))
                .build();
    }

        private SecurityContext securityContext() {
            return SecurityContext.builder().securityReferences(defaultAuth()).build();
        }

        private List<SecurityReference> defaultAuth() {
            AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
            AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
            authorizationScopes[0] = authorizationScope;
            return Collections.singletonList(new SecurityReference("Authorization", authorizationScopes));
        }

        private ApiKey apiKey() {
            return new ApiKey("Authorization", "Authorization", "header");
        }





    //配置swagger信息 apiInfo
    public ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact(
                "刘世泽",
                "http://baidu.com",
                "1714458266@qq.com");

        return  new ApiInfo(
                "愚者先生的swaggerAPI日记",
                "赞美愚者！！",
                "v5.3",
                "http://www.baidu.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
        );
    }



    }




